//
//  KRAboutWeViewController.m
//  KEREN
//
//  Created by MW on 2023/4/19.
//

#import "KRAboutWeViewController.h"
#import "KRSettingCell.h"
@interface KRAboutWeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, assign) long long timeStamp;
@property (nonatomic, assign) int tap_times;
@end

@implementation KRAboutWeViewController

- (void)showShareAlert:(NSString *)logFilePath{
    NSData *data = [[NSData alloc] initWithContentsOfFile:logFilePath];
    NSURL *tempUrl = [NSURL fileURLWithPath:logFilePath];
    if (!data) {
        return;
    }
    NSArray *activityItemsArray = @[data,tempUrl];
    // 2、初始化控制器，添加分享内容至控制器
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:nil];
    activityVC.modalInPopover = YES;
    // ios8.0 之后用此方法回调
    UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
        NSLog(@"activityType == %@",activityType);
        if (completed == YES) {
            NSLog(@"completed");
        }else{
            NSLog(@"取消");
        }
    };
    activityVC.completionWithItemsHandler = itemsBlock;
    // 4、调用控制器
    [self presentViewController:activityVC animated:YES completion:nil];
}

- (NSString *)fileSizeToString:(long long)dataSize{
    float m_data = dataSize/(1024*1024.0);
    NSString *fileStr = [NSString stringWithFormat:@"%.2fM",m_data];
    return fileStr;
}

-(void)showAlertSheet{
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"app运行日志" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    DDLogFileManagerDefault *manager = [appdelegate logFileManager];
    NSArray *array =manager.sortedLogFileInfos;
    WEAKSELF
    for(DDLogFileInfo *logInfo in array){
        NSLog(@"file path is %@,file data is %llu",logInfo.filePath,logInfo.fileSize);
        NSArray *seperate_array = [logInfo.fileName componentsSeparatedByString:@" "];
        NSString *name = @"";
        if(seperate_array.count == 2){
            name = [NSString stringWithFormat:@"%@,文件大小:%@",seperate_array[1],[self fileSizeToString:logInfo.fileSize]];
        }
        UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:name style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf showShareAlert:logInfo.filePath];
        }];
        [actionSheetController addAction:showAllInfoAction];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
    }];
    [actionSheetController addAction:cancelAction];
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

#pragma mark-tap
-(void)tap:(UITapGestureRecognizer *)gesture{
    long long timeStamp = [[NSDate date] timeIntervalSince1970];
    if(timeStamp - self.timeStamp > 1){
        self.tap_times = 1;
    }else{
        self.tap_times++;
    }
    self.timeStamp = timeStamp;
    if(self.tap_times >= 10){
        [self showAlertSheet];
    }
}

#pragma mark-

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavViewWithRightTitle:@"" navColorIswhite:YES withTitle:@"关于我们"];
    UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake((DEVICE_WIDTH - 198) / 2, 116 + STATUSBAR_HEIGHT, 198, 46)];
    iconImageView.userInteractionEnabled= YES;
    [self.view addSubview:iconImageView];
    iconImageView.image = [UIImage imageNamed:@"logo_black"];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [iconImageView addGestureRecognizer:tap];
    
    _titleArr = @[@"用户注册协议",@"隐私政策",@"服务电话",@"当前版本"];
    self.tableView.tableFooterView = [self footerView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KRSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KRSettingCell"];
    cell.nameLbl.text = _titleArr[indexPath.row];
    cell.rightIcon.hidden = NO;
    cell.photoIcon.hidden = YES;
    cell.decLbl.text = @"";
    if(indexPath.row == _titleArr.count - 1){
        cell.lineView.hidden = YES;
    }else{
        cell.lineView.hidden = NO;
    }
    if(indexPath.row >1){
        cell.rightIcon.hidden = YES;
        cell.decLeftConstraint.constant = 0;
        if(indexPath.row == 2){
            cell.decLbl.text = @"400-6634-576";
        }else{
            cell.decLbl.text = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(indexPath.row == 0){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@/html/userAgreement.html",BaseHost]] options:@{} completionHandler:^(BOOL success) {
        }];

    }else if (indexPath.row == 1){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@/html/privacyAgreement.html",BaseHost]] options:@{} completionHandler:^(BOOL success) {
        }];
    }else if(indexPath.row == 2){
//        NSMutableString *str= [[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"400 6634 576"];
//        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:^(BOOL success) {
//        }];
    }
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVTOP_Height + 164, DEVICE_WIDTH, DEVICE_HEIGHT - NAVTOP_Height - 164)];
        _tableView.backgroundColor = Main_BackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 60;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"KRSettingCell" bundle:nil] forCellReuseIdentifier:@"KRSettingCell"];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (UIView *)footerView{
    if(!_footerView){
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 160)];
        UILabel *tipsLbl = [UILabel setLblText:@"Copyright ©2022 可人\n All Rights Reserved" font:Font(14) withColor:Color_86898E inSubView:_footerView];
        tipsLbl.textAlignment = NSTextAlignmentCenter;
        tipsLbl.numberOfLines = 0;
        [tipsLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.centerY.equalTo(_footerView);
        }];
        
    }
    return _footerView;
}


@end
